﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using MarketOnlineDTO;
using MySql.Data.MySqlClient;

namespace MarketOnlineDAO
{
    public class ChiTietHoaDonDAO : BaseDataProvider
    {
        MySqlCommand _cmd;
        public Boolean ThemChiTietHoaDon(ChiTietHoaDonDTO ct)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "insert into ChiTietHoaDon(MaHD,MaSP,SoLuong,DonGia) values(@MaHD,@MaSP,@SoLuong,@DonGia)";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaHD", ct.MaHD));
            _cmd.Parameters.Add(new MySqlParameter("@MaSP", ct.MaSP));
            _cmd.Parameters.Add(new MySqlParameter("@SoLuong", ct.SoLuong));
            _cmd.Parameters.Add(new MySqlParameter("@DonGia", ct.DonGia));


            return ExecuteNonQuery(_cmd);
        }
        public List<ChiTietHoaDonDTO> LayDanhSachChiTietHoaDon()
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from ChiTietHoaDon";
            _cmd.CommandType = CommandType.Text;

            DataTable dt = new DataTable();

            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }

            List<ChiTietHoaDonDTO> list = new List<ChiTietHoaDonDTO>();

            foreach (DataRow row in dt.Rows)
            {
                ChiTietHoaDonDTO dto = new ChiTietHoaDonDTO();
                dto.MaCTHD = int.Parse(row["MaCTHD"].ToString());
                dto.MaHD = int.Parse(row["MaHD"].ToString());
                dto.MaSP = int.Parse(row["MaSP"].ToString());
                dto.SoLuong = int.Parse(row["SoLuong"].ToString());
                dto.DonGia = double.Parse(row["DonGia"].ToString());
                

                list.Add(dto);
            }

            return list;
        }

//-------------Quân viết thêm--------------------------------------------------

        public List<ChiTietHoaDonDTO> LayCTHDTheoMaHD(ChiTietHoaDonDTO ct)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from ChiTietHoaDon where MaHD = @MaHD";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaCTHD", ct.MaHD));

            DataTable dt = new DataTable();

            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }

            List<ChiTietHoaDonDTO> list = new List<ChiTietHoaDonDTO>();

            foreach (DataRow row in dt.Rows)
            {
                ChiTietHoaDonDTO dto = new ChiTietHoaDonDTO();
                dto.MaCTHD = int.Parse(row["MaCTHD"].ToString());
                dto.MaHD = int.Parse(row["MaHD"].ToString());
                dto.MaSP = int.Parse(row["MaSP"].ToString());
                dto.SoLuong = int.Parse(row["SoLuong"].ToString());
                dto.DonGia = double.Parse(row["DonGia"].ToString());


                list.Add(dto);
            }

            return list;
        }

//-------------------------------------------------------------------------------

        public ChiTietHoaDonDTO LayChiTietHoaDon(int MaChiTietHoaDon)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from ChiTietHoaDon where MaCTHD = @MaCTHD";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaCTHD", MaChiTietHoaDon.ToString()));


            DataTable dt = new DataTable();
            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }

            ChiTietHoaDonDTO ct = new ChiTietHoaDonDTO();

            try
            {
                
                DataRow row = dt.Rows[0];//kết quả trả ra ở dòng 0

                ct.MaCTHD = int.Parse(row["MaCTHD"].ToString());
                ct.MaHD = int.Parse(row["MaHD"].ToString());
                ct.MaSP = int.Parse(row["MaSP"].ToString());
                ct.SoLuong = int.Parse(row["SoLuong"].ToString());
                ct.DonGia = double.Parse(row["DonGia"].ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return ct;
        }

       public Boolean CapNhatChiTietHoaDon(ChiTietHoaDonDTO ct)
       {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "update ChiTietHoaDon set SoLuong = @SoLuong , DonGia = @DonGia , MaHD = @MaHD , MaSP = @MaSP where MaCTHD = @MaCTHD";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaHD", ct.MaHD));
            _cmd.Parameters.Add(new MySqlParameter("@MaSP", ct.MaSP));
            _cmd.Parameters.Add(new MySqlParameter("@SoLuong", ct.SoLuong));
            _cmd.Parameters.Add(new MySqlParameter("@DonGia", ct.DonGia));
            _cmd.Parameters.Add(new MySqlParameter("@MaCTHD", ct.MaCTHD));

            return ExecuteNonQuery(_cmd);
       }

       public Boolean XoaChiTietHoaDon(int  MaChiTietHoaDon)
       {
           _cmd = CreateMysqlCommand();
           _cmd.CommandText = "delete from ChiTietHoaDon where MaCTHD = @MaCTHD";
           _cmd.CommandType = CommandType.Text;
           _cmd.Parameters.Add(new MySqlParameter("@MaCTHD", MaChiTietHoaDon.ToString()));

           return ExecuteNonQuery(_cmd);
       }


       public Boolean XoaChiTietHoaDon_MaHD(int MaHD)
       {
           _cmd = CreateMysqlCommand();
           _cmd.CommandText = "delete from ChiTietHoaDon where MaHD = @MaHD";
           _cmd.CommandType = CommandType.Text;
           _cmd.Parameters.Add(new MySqlParameter("@MaHD", MaHD));

           return ExecuteNonQuery(_cmd);
       }
        
    }
}
